package ch.uzh.ifi.sonar.qacpp;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

import ch.uzh.ifi.sonar.util.SonarConstant;

public class StreamsSummaryFileWriter {


	public static final String CSV_SEP = SonarConstant.CSV_SEP;

	private long STCYC = 0, STDIT = 0, STGTO = 0, STLCM = 0, STLIN = 0,
			STMIF = 0, STMTH = 0, STNOC = 0, STNOP = 0, STOPT = 0, STSUB = 0,
			STTLN = 0, STVAR = 0, STWMC = 0, STXLN = 0;

	private FileWriter writer;

	public StreamsSummaryFileWriter(String summaryPath) throws IOException {
		setWriter(new FileWriter(summaryPath));
	}

	public void writeHeader() throws IOException {
		writer.append("Stream Name ");
		writer.append(CSV_SEP);
		writer.append("Year ");
		writer.append(CSV_SEP);
		writer.append("Source File No.");
		writer.append(CSV_SEP);
		writer.write("STCYC");
		writer.append(CSV_SEP);
		writer.write("STDIT");
		writer.append(CSV_SEP);
		writer.write("STGTO");
		writer.append(CSV_SEP);
		writer.write("STLCM");
		writer.append(CSV_SEP);
		writer.write("STLIN");
		writer.append(CSV_SEP);
		writer.write("STMIF");
		writer.append(CSV_SEP);
		writer.write("STMTH");
		writer.append(CSV_SEP);
		writer.write("STNOC");
		writer.append(CSV_SEP);
		writer.write("STNOP");
		writer.append(CSV_SEP);
		writer.write("STOPT");
		writer.append(CSV_SEP);
		writer.write("STSUB");
		writer.append(CSV_SEP);
		writer.write("STTLN");
		writer.append(CSV_SEP);
		writer.write("STVAR");
		writer.append(CSV_SEP);
		writer.write("STWMC");
		writer.append(CSV_SEP);
		writer.write("STXLN");
		writer.append(System.getProperty("line.separator"));
	}

	private void initAllMetrics() {
		STCYC = 0;
		STDIT = 0;
		STGTO = 0;
		STLCM = 0;
		STLIN = 0;
		STMIF = 0;
		STMTH = 0;
		STNOC = 0;
		STNOP = 0;
		STOPT = 0;
		STSUB = 0;
		STTLN = 0;
		STVAR = 0;
		STWMC = 0;
		STXLN = 0;
	}

	public void writeContent(File streamAnalysisFile) {
		FileInputStream fis;
		try {
			fis = new FileInputStream(streamAnalysisFile);
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					fis));
			String line;
			int lineCount = 0;
			initAllMetrics();
			while ((line = reader.readLine()) != null) {
				lineCount++;
				if (lineCount > 1) {
					String[] lineArr = line.split(CSV_SEP);
					/**
					 * 0: Module 1: Sub Module 2: File 3: STCYC 4: STDIT 5:STGTO
					 * 6: STLCM 7: STLIN 8: STMIF 9: STMTH 10: STNOC 11:STNOP
					 * 12:STOPT 13: STSUB 14: STTLN 15: STVAR 16: STWMC 17:
					 * STXLN
					 */
					STCYC += Long.valueOf(lineArr[3]);
					STDIT += Long.valueOf(lineArr[4]);
					STGTO += Long.valueOf(lineArr[5]);
					STLCM += Long.valueOf(lineArr[6]);
					STLIN += Long.valueOf(lineArr[7]);
					STMIF += Long.valueOf(lineArr[8]);
					STMTH += Long.valueOf(lineArr[9]);
					STNOC += Long.valueOf(lineArr[10]);
					STNOP += Long.valueOf(lineArr[11]);
					STOPT += Long.valueOf(lineArr[12]);
					STSUB += Long.valueOf(lineArr[13]);
					STTLN += Long.valueOf(lineArr[14]);
					STVAR += Long.valueOf(lineArr[15]);
					STWMC += Long.valueOf(lineArr[16]);
					STXLN += Long.valueOf(lineArr[17]);
				}
			}
			String fileName = streamAnalysisFile.getName();
			String streamName = fileName.substring(0, fileName.lastIndexOf(".csv"));
			String year = streamName.substring(streamName.indexOf("(")+1, streamName.lastIndexOf(")"));
			writer.append(streamName);
			writer.append(CSV_SEP);
			writer.append(year);
			writer.append(CSV_SEP);
			writer.append(Integer.toString(lineCount));
			writer.append(CSV_SEP);
			writer.append(Long.toString(STCYC));
			writer.append(CSV_SEP);
			writer.append(Long.toString(STDIT));
			writer.append(CSV_SEP);
			writer.append(Long.toString(STGTO));
			writer.append(CSV_SEP);
			writer.append(Long.toString(STLCM));
			writer.append(CSV_SEP);
			writer.append(Long.toString(STLIN));
			writer.append(CSV_SEP);
			writer.append(Long.toString(STMIF));
			writer.append(CSV_SEP);
			writer.append(Long.toString(STMTH));
			writer.append(CSV_SEP);
			writer.append(Long.toString(STNOC));
			writer.append(CSV_SEP);
			writer.append(Long.toString(STNOP));
			writer.append(CSV_SEP);
			writer.append(Long.toString(STOPT));
			writer.append(CSV_SEP);
			writer.append(Long.toString(STSUB));
			writer.append(CSV_SEP);
			writer.append(Long.toString(STTLN));
			writer.append(CSV_SEP);
			writer.append(Long.toString(STVAR));
			writer.append(CSV_SEP);
			writer.append(Long.toString(STWMC));
			writer.append(CSV_SEP);
			writer.append(Long.toString(STXLN));
			writer.append(System.getProperty("line.separator"));	

		} catch (FileNotFoundException e) {
			e.printStackTrace();

		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public void flush() throws IOException {
		writer.flush();
	}

	public void close() throws IOException {
		writer.close();
	}

	public void append(CharSequence c) throws IOException {
		writer.append(c);
	}

	public void appendWithSeparator(CharSequence c) throws IOException {
		writer.append(c);
		writer.append(CSV_SEP);
	}

	public void appendWithEnd(CharSequence c) throws IOException {
		writer.append(c);
		writer.append(System.getProperty("line.separator"));
	}

	public FileWriter getWriter() {
		return writer;
	}

	public void setWriter(FileWriter writer) {
		this.writer = writer;
	}

}
